package ftp;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Driver;
import java.util.concurrent.ConcurrentHashMap;

import main.MusicFile;
import main.Transitions;

public class FTPController  {


	private static FTPController ftpManager;
 
	static
	{
		ftpManager = new FTPController();	 
	}

	static ConcurrentHashMap<MusicFile, FTPTransferListener> activeUploads= new ConcurrentHashMap<MusicFile, FTPTransferListener>();


	public String getRemoteIp() {
		return remoteIp;
	}
	public void setRemoteIp(String remoteIp) {
		this.remoteIp = remoteIp;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	private static String remoteIp = "209.190.85.253";
	private static String username = "b8_10737801";
	private static String password = "notowar";


	public static FTPController getInstance() {
		return ftpManager;
	}

	public  void send(MusicFile transferMusicFile) throws IllegalStateException, FileNotFoundException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException {

		File transferFile = (transferMusicFile.referenceFile);
		FTPTransferListener listener = new FTPTransferListener(transferFile);  
		FTPClient ftpClient = new FTPClient();
		ftpClient.connect(remoteIp);
		ftpClient.login(username, password);
 		ftpClient.changeDirectory("sabahattin-tuncer.com/htdocs/shakey");			
		ftpClient.upload(transferFile,listener);
			
			
			
			//					System.out.print(transferFile.getName());
			// 			try {		ftpClient.rename(ftpClient.currentDirectory() + "/" + transferFile.getName() ,transferFile.getName() +
			// 						+ transferMusicFile.airDate.get(Calendar.DAY_OF_MONTH)+"/"+transferMusicFile.airDate.get(Calendar.MONTH)+"/"+ transferMusicFile.airDate.get(Calendar.YEAR));
			// 			}catch (Exception e) {
			// 				e.printStackTrace();
			// 			}
			//		 
		
	}

	public int getPercentageComplete(MusicFile m) {
		return activeUploads.get(m).percentage;
	}

	static void update(FTPTransferListener l) {
		  main.Driver.updateUI();
	}
	
	
	}

 